package com.batman.baselibrary.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.batman.baselibrary.R;
import com.ui.widget.UIProgressBar;

import java.lang.ref.WeakReference;

public class CustomCountDownDialog extends Dialog {

    protected static final int STOP = 0x10000;
    protected static final int NEXT = 0x10001;

    public TextView tvTitle;
    public TextView tvMessage;

    public UIProgressBar mCircleProgressBar;

    private ProgressHandler myHandler = new ProgressHandler();

    public CustomCountDownDialog(Context context) {
        super(context, R.style.CustomDialog);
        init(context);
    }

    public CustomCountDownDialog(Context context, int theme) {
        super(context, R.style.CustomDialog);
        init(context);
    }

    private void init(Context context) {
        setCancelable(false);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = LayoutInflater.from(context).inflate(
                R.layout.dialog_custom, null);
        tvTitle = (TextView) view.findViewById(R.id.tv_tile);

        tvMessage = (TextView) view.findViewById(R.id.tv_message);
        mCircleProgressBar = (UIProgressBar) view.findViewById(R.id.circleProgressBar);
        mCircleProgressBar.setUIProgressBarTextGenerator(new UIProgressBar.UIProgressBarTextGenerator() {
            @Override
            public String generateText(UIProgressBar progressBar, int value, int maxValue) {
                return (value * 3 / maxValue) + "";
            }
        });

        myHandler.setProgressBar(mCircleProgressBar);

        setContentView(view);

    }

    public TextView getTvTitle() {
        return tvTitle;
    }

    public void setTvTitle(String title) {
        this.tvTitle.setText(title);
    }

    public TextView getTvMessage() {
        return tvMessage;
    }

    public void setTvMessage(String message) {
        this.tvMessage.setText(message);
    }

    public UIProgressBar getCircleProgressBar() {
        return mCircleProgressBar;
    }

    /**
     * 设置最大值
     *
     * @param maxValue
     */
    public void setMaxCircleProgressBar(int maxValue) {
        mCircleProgressBar.setMaxValue(maxValue);
    }

    /**
     * 设置初始值
     *
     * @param value
     */
    public void setStartCircleProgressBar(int value) {
        mCircleProgressBar.setProgress(value);
    }

    /**
     * @param value
     */
    public void setCircleProgressBar(int value) {
        Message msg = new Message();
        msg.what = NEXT;
        msg.arg1 = value;
        myHandler.sendMessage(msg);

    }


    private static class ProgressHandler extends Handler {
        private WeakReference<UIProgressBar> weakCircleProgressBar;

        void setProgressBar(UIProgressBar circleProgressBar) {
            weakCircleProgressBar = new WeakReference<>(circleProgressBar);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case STOP:
                    break;
                case NEXT:
                    if (!Thread.currentThread().isInterrupted()) {
                        if (weakCircleProgressBar.get() != null) {
                            weakCircleProgressBar.get().setProgress(msg.arg1);
                        }
                    }
            }

        }
    }
}

